import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class NativeSdkTestPage extends StatefulWidget {
  const NativeSdkTestPage({Key? key}) : super(key: key);

  @override
  State<NativeSdkTestPage> createState() => _NativeSdkTestPageState();
}

class _NativeSdkTestPageState extends State<NativeSdkTestPage> {
  static const _channel = MethodChannel('cn.lstest.flutter_bluetooth/native_sdk');
  String _output = 'Not checked yet.';
  Map<String, bool> _results = {};
  bool _loading = false;

  Future<void> _checkNativeClasses() async {
    // Put the fully-qualified class names you expect inside the AARs here.
    // You need to update these names to match the vendor SDK classes.
    final classNames = <String>[
      'com.jieli.lib.bluetooth.rcsp.BluetoothManager',
      'com.jieli.audio.AudioEngine',
      'com.jieli.other.SomeClass'
    ];

    setState(() {
      _loading = true;
      _output = 'Checking...';
      _results = {};
    });

    try {
      final dynamic raw = await _channel.invokeMethod('checkNativeClasses', {'classes': classNames});
      final Map<dynamic, dynamic> result = (raw is Map) ? raw : <dynamic, dynamic>{};
      final Map<String, bool> mapped = {};
      result.forEach((k, v) {
        final key = k?.toString() ?? '';
        final val = (v is bool) ? v : (v.toString().toLowerCase() == 'true');
        if (key.isNotEmpty) mapped[key] = val;
      });
      setState(() {
        _results = mapped;
        _output = _results.isEmpty ? 'No results' : '';
      });
    } on PlatformException catch (e) {
      setState(() {
        _output = 'PlatformException: ${e.message}';
      });
    } finally {
      setState(() {
        _loading = false;
      });
    }
  }

  @override
  void initState() {
    super.initState();
    // Auto-run the check when the page loads
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _checkNativeClasses();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Native SDK Test')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            ElevatedButton.icon(
              onPressed: _loading ? null : _checkNativeClasses,
              icon: const Icon(Icons.refresh),
              label: Text(_loading ? 'Checking...' : 'Refresh'),
            ),
            const SizedBox(height: 12),
            Expanded(
              child: _results.isEmpty
                  ? Center(child: Text(_output))
                  : ListView.builder(
                      itemCount: _results.length,
                      itemBuilder: (context, index) {
                        final key = _results.keys.elementAt(index);
                        final available = _results[key] ?? false;
                        return ListTile(
                          leading: Icon(
                            available ? Icons.check_circle : Icons.cancel,
                            color: available ? Colors.green : Colors.red,
                          ),
                          title: Text(key),
                          subtitle: Text(available ? 'Available' : 'Not found'),
                        );
                      },
                    ),
            ),
          ],
        ),
      ),
    );
  }
}
